window.onload = initalizePage;
window.onunload = function() {}
var xhr = false;


function initalizePage() {
	var select_sort = document.getElementById('sort');
	select_sort.onchange = fixSortUrl;
	var select_sort2 = document.getElementById('sort2');
	select_sort2.onchange = fixSortUrl;

	var select_itemPerPage = document.getElementById('itemNum');
	select_itemPerPage.onchange = fixPerPageUrl;
	var select_itemPerPage2 = document.getElementById('itemNum2');
	select_itemPerPage2.onchange = fixPerPageUrl;
}

function getXhr() {
	if (xhr) { return xhr; }
	else { //set xhr
		if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); }
		else {
			if(window.ActiveXObject) {
				try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (e) {
					try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
					catch (e) {}
				}
			}
		}
		return xhr;
	}
}


function fixSortUrl() {
	fixUrl(this.value, 'sort');
}

function fixPerPageUrl() {
	fixUrl(this.value, 'per_page');
}

function fixUrl(newVal, urlOpt) {
	search_arr = window.location.search.substring(1).split('&');

	var niceArr = [];
	for (var intgr in search_arr) {
		var keyVal = search_arr[intgr].split('=');
		niceArr[keyVal[0]] = keyVal[1];
	}
	niceArr[urlOpt] = newVal;

	var out_search_arr = []
	for (var key in niceArr) {
		var temp_str = key + '=' + niceArr[key];
		out_search_arr.push(temp_str);
	}

	window.location.search = '?' + out_search_arr.join('&');
}